package com.jmu.edu.IMApplication.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.adapter.AlbumItemAdapter;
import com.jmu.edu.IMApplication.adapter.EmojiItemAdapter;
import com.jmu.edu.IMApplication.bean.Emoji;
import com.jmu.edu.IMApplication.bean.EmojiList;
import com.jmu.edu.IMApplication.utils.SystemUtil;

import java.util.ArrayList;


public class EmojiFragment extends Fragment {

    private RecyclerView recyclerView;
    private ArrayList<Emoji> emojiList;
    private EmojiItemAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_emoji, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        recyclerView = getView().findViewById(R.id.fragment_emoji_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 6);
        recyclerView.setLayoutManager(gridLayoutManager);
        loadData();
    }

    private void loadData() {
        String json = SystemUtil.getJson("EmojiList.json", getActivity());
        EmojiList tempEmojiList = new Gson().fromJson(json, EmojiList.class);
        emojiList = tempEmojiList.getEmoji_list();
        if (adapter == null) {
            adapter = new EmojiItemAdapter();
        }
        adapter.setEmojiList(emojiList);
        // 如果还没有添加适配器，那么设置适配器，否则适配器提醒recyclerview进行数据更新
        if (recyclerView.getAdapter() == null) {
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }
}